<?php

declare(strict_types=1);
/**
 * 
 */
namespace WanZ\App\User\Service;

use Hyperf\Database\Model\Builder;
use WanZ\App\User\Contract\UserBlackContract;
use WanZ\App\User\Model\UserBlack;
use MoChat\Framework\Service\AbstractService;

class UserBlackService extends AbstractService implements UserBlackContract
{
    /**
     * @var UserBlack
     */
    protected $model;
    
    /**
     * @var DEFAULT_FIELDS
     */
    private const DEFAULT_FIELDS = ['id', 'user_phone', 'limit_type', 'created_at'];    
    
    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getUserById(int $id, array $columns = []): array
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 查询多条 - 根据手机号.
     * @param array $userPhone 手机号
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getUsersByUserPhone(string $userPhone, array $columns = []): array
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        $res = $this->model::query()
            ->where('user_phone', $userPhone)
            ->get($columns);

        $res || $res = collect([]);

        return $res->toArray();
    }

    /**
     * 按条件计算用户总数.
     * @param array $where 查询条件
     * @return int 返回值
     */
    public function countUsers(array $where = [], array $options = []): int
    {
        $model = $this->model->optionWhere($where, $options);
        return $model->count();
    }

    /**
     * 删除 - 单条
     * @param int $id 删除ID
     * @return int 删除条数
     */
    public function deleteUser(int $id): int
    {
        return $this->model->deleteOne($id);
    }
    
}    
